/* eslint-disable array-callback-return */
import { message } from 'antd';
import {
  getEmpList,
  getBaseInfo,
  getAddTaskList,
  getReduceTaskList,
  getAdjustTaskList,
  getEmpInsPayInfo,
  getEmpAccuPayInfo,
  getEmpInsSerInfo,
  getEmpInsRefundInfo,
  getWageInfoList,
  getEmpLaborContInfo,
  getCustList,
  getCustBaseInfo,
  getCustContList,
  getCrmquoList,
  getOnceChargeList,
  getBillList,
} from '@/services/integratedquery';

export default {
  state: {
    empBaseInfo: {}, // 员工基本信息
    addTaskList: {
      // 增员任务列表数据
      list: [],
    },
    reduceTaskList: {
      // 减员任务列表数据
      list: [],
    },
    adjustTaskList: {
      // 调整任务列表数据
      list: [],
    },
    empInsPayList: {
      // 社保实缴列表数据
      list: [],
    },
    empAccuPayList: {
      // 公积金实缴列表数据
      list: [],
    },
    empInsSerInfoList: {
      // 社保服务办理列表数据
      list: [],
    },
    empInsRefundInfoList: {
      // 返款信息列表数据
      list: [],
    },
    wageDataList: {
      // 代发工资列表数据
      list: [],
    },
    empLaborContInfoList: {}, // 劳动合同信息数据
    custBaseInfo: {}, // 客户基本信息
    custConstList: {
      // 商务合同列表数据
      list: [],
    },
    crmquoDataList: {
      // 报价单列表数据
      list: [],
    },
    onceChargeDataList: {
      // 一次性收费列表数据
      list: [],
    },
    billList: {
      // 客户账单列表数据
      list: [],
    },
    type: 1, // 区分客户，雇员
    custLabelObj: undefined, // 搜索客户默认值
    empLabelObj: undefined, // 搜索雇员默认值
  },
  effects: {
    // 获取雇员列表
    *getEmpList({ payload, callback = () => {} }, { call }) {
      const response = yield call(getEmpList, payload);
      callback(response);
    },
    // 获取员工基本信息
    *getBaseInfo({ payload }, { call, put }) {
      const response = yield call(getBaseInfo, payload);
      yield put({
        type: 'getBaseList',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getBaseList',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取员工基本信息错误：服务器端错误');
      }
    },
    // 获取增员任务列表
    *getAddTaskList({ payload }, { call, put }) {
      const response = yield call(getAddTaskList, payload);
      yield put({
        type: 'getAddTask',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getAddTask',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取增员任务列表数据错误：服务器端错误');
      }
    },
    // 获取减员任务列表
    *getReduceTaskList({ payload }, { call, put }) {
      const response = yield call(getReduceTaskList, payload);
      yield put({
        type: 'getReduceTask',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getReduceTask',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取减员任务列表数据错误：服务器端错误');
      }
    },
    // 获取调整任务列表
    *getAdjustTaskList({ payload }, { call, put }) {
      const response = yield call(getAdjustTaskList, payload);
      yield put({
        type: 'getAdjustTask',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getAdjustTask',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取调整任务列表数据错误：服务器端错误');
      }
    },
    // 获取社保实缴列表
    *getEmpInsPayInfo({ payload }, { call, put }) {
      const response = yield call(getEmpInsPayInfo, payload);
      yield put({
        type: 'empInsPayInfo',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'empInsPayInfo',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取社保实缴列表数据错误：服务器端错误');
      }
    },
    // 获取公积金实缴列表
    *getEmpAccuPayInfo({ payload }, { call, put }) {
      const response = yield call(getEmpAccuPayInfo, payload);
      yield put({
        type: 'empAccuPayInfo',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'empAccuPayInfo',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取公积金实缴列表数据错误：服务器端错误');
      }
    },
    // 获取社保服务办理列表
    *getEmpInsSerInfo({ payload }, { call, put }) {
      const response = yield call(getEmpInsSerInfo, payload);
      yield put({
        type: 'empInsSerInfo',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'empInsSerInfo',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取社保服务办理列表数据错误：服务器端错误');
      }
    },
    // 获取返款信息列表
    *getEmpInsRefundInfo({ payload }, { call, put }) {
      const response = yield call(getEmpInsRefundInfo, payload);
      yield put({
        type: 'empInsRefundInfo',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'empInsRefundInfo',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取返款信息列表数据错误：服务器端错误');
      }
    },
    // 获取代发工资列表
    *getWageInfoList({ payload }, { call, put }) {
      const response = yield call(getWageInfoList, payload);
      yield put({
        type: 'getWageInfoData',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getWageInfoData',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取代发工资列表数据错误：服务器端错误');
      }
    },
    // 获取劳动合同信息
    *getEmpLaborContInfo({ payload }, { call, put }) {
      const response = yield call(getEmpLaborContInfo, payload);
      yield put({
        type: 'empLaborContInfo',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'empLaborContInfo',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取劳动合同信息数据错误：服务器端错误');
      }
    },
    // 获取客户列表
    *getCustList({ payload, callback = () => {} }, { call }) {
      const response = yield call(getCustList, payload);
      callback(response);
    },
    // 获取客户基本信息
    *getCustBaseInfo({ payload }, { call, put }) {
      const response = yield call(getCustBaseInfo, payload);
      yield put({
        type: 'getCustBaseList',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getCustBaseList',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取客户基本信息错误：服务器端错误');
      }
    },
    // 获取商务合同列表数据
    *getCustContList({ payload }, { call, put }) {
      const response = yield call(getCustContList, payload);
      yield put({
        type: 'getCustCont',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getCustCont',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取商务合同列表数据错误：服务器端错误');
      }
    },
    // 获取报价单列表数据
    *getCrmquoList({ payload }, { call, put }) {
      const response = yield call(getCrmquoList, payload);
      yield put({
        type: 'getCrmquoData',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getCrmquoData',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取报价单列表数据错误：服务器端错误');
      }
    },
    // 获取一次性收费列表数据
    *getOnceChargeList({ payload }, { call, put }) {
      const response = yield call(getOnceChargeList, payload);
      yield put({
        type: 'getOnceChargeData',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getOnceChargeData',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取一次性收费列表数据错误：服务器端错误');
      }
    },
    // 获取客户账单列表数据
    *getBillList({ payload }, { call, put }) {
      const response = yield call(getBillList, payload);
      yield put({
        type: 'getBillData',
        payload: {
          data: {},
        },
      });
      if (response.success) {
        yield put({
          type: 'getBillData',
          payload: {
            data: response.data,
          },
        });
      } else {
        message.warning(response.message || '获取客户账单列表数据错误：服务器端错误');
      }
    },
  },

  reducers: {
    // 获取员工基本信息数据
    getBaseList(
      state,
      {
        payload: { data },
      }
    ) {
      return {
        ...state,
        empBaseInfo: data,
      };
    },
    // 获取增员任务列表数据
    getAddTask(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        addTaskList: {
          list,
        },
      };
    },
    // 获取减员任务列表数据
    getReduceTask(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        reduceTaskList: {
          list,
        },
      };
    },
    // 获取调整任务列表数据
    getAdjustTask(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        adjustTaskList: {
          list,
        },
      };
    },
    // 获取社保实缴列表数据
    empInsPayInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      const pageArr = [];
      if (list.length) {
        list.map(item => {
          if (item.empAssList.length) {
            // 基数 empBase+ unitBase
            // 个人比例  empProp
            // 个人缴费  empAmo
            // 公司比例  unitProp
            // 公司缴费  unitAmo
            const obj = {};
            item.empAssList.map(c => {
              obj[`empBase_${c.ass}`] = c.empBase || c.empBase === 0 ? c.empBase : '--';
              obj[`unitBase_${c.ass}`] = c.unitBase || c.unitBase === 0 ? c.unitBase : '--';
              obj[`empProp_${c.ass}`] = c.empProp || c.empProp === 0 ? c.empProp : '--';
              obj[`empAmo_${c.ass}`] = c.empAmo || c.empAmo === 0 ? c.empAmo : '--';
              obj[`unitProp_${c.ass}`] = c.unitProp || c.unitProp === 0 ? c.unitProp : '--';
              obj[`unitAmo_${c.ass}`] = c.unitAmo || c.unitAmo === 0 ? c.unitAmo : '--';
              obj[`indvAccoNo_${c.ass}`] = c.indvAccoNo || c.indvAccoNo === 0 ? c.indvAccoNo : '--';
            });
            pageArr.push({ ...item, ...obj });
          }
        });
      }
      return {
        ...state,
        empInsPayList: {
          list: pageArr,
        },
      };
    },
    // 获取公积金实缴列表数据
    empAccuPayInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      const pageArr = [];
      if (list.length) {
        list.map(item => {
          if (item.empAssList.length) {
            // 基数 empBase+ unitBase
            // 个人比例  empProp
            // 个人缴费  empAmo
            // 公司比例  unitProp
            // 公司缴费  unitAmo
            const obj = {};
            item.empAssList.map(c => {
              obj[`empBase_${c.ass}`] = c.empBase || c.empBase === 0 ? c.empBase : '--';
              obj[`unitBase_${c.ass}`] = c.unitBase || c.unitBase === 0 ? c.unitBase : '--';
              obj[`empProp_${c.ass}`] = c.empProp || c.empProp === 0 ? c.empProp : '--';
              obj[`empAmo_${c.ass}`] = c.empAmo || c.empAmo === 0 ? c.empAmo : '--';
              obj[`unitProp_${c.ass}`] = c.unitProp || c.unitProp === 0 ? c.unitProp : '--';
              obj[`unitAmo_${c.ass}`] = c.unitAmo || c.unitAmo === 0 ? c.unitAmo : '--';
            });
            pageArr.push({ ...item, ...obj });
          }
        });
      }
      return {
        ...state,
        empAccuPayList: {
          list: pageArr,
        },
      };
    },
    // 获取社保服务办理列表数据
    empInsSerInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        empInsSerInfoList: {
          list,
        },
      };
    },
    // 获取返款信息列表数据
    empInsRefundInfo(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        empInsRefundInfoList: {
          list,
        },
      };
    },
    // 获取代发工资列表数据
    getWageInfoData(
      state,
      {
        payload: { data },
      }
    ) {
      const list = Array.isArray(data) ? data : [];
      return {
        ...state,
        wageDataList: {
          list,
        },
      };
    },
    // 获取劳动合同信息列表数据
    empLaborContInfo(
      state,
      {
        payload: { data },
      }
    ) {
      return {
        ...state,
        empLaborContInfoList: data,
      };
    },
    // 获取客户基本信息数据
    getCustBaseList(
      state,
      {
        payload: { data },
      }
    ) {
      return {
        ...state,
        custBaseInfo: data,
      };
    },
    // 获取商务合同列表数据
    getCustCont(
      state,
      {
        payload: { data },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        custConstList: {
          list,
        },
      };
    },
    // 获取报价单列表数据
    getCrmquoData(
      state,
      {
        payload: { data },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        crmquoDataList: {
          list,
        },
      };
    },
    // 获取一次性收费列表数据
    getOnceChargeData(
      state,
      {
        payload: { data },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        onceChargeDataList: {
          list,
        },
      };
    },
    // 获取客户账单列表数据
    getBillData(
      state,
      {
        payload: { data },
      }
    ) {
      const list = data.pageRecords && Array.isArray(data.pageRecords) ? data.pageRecords : [];
      return {
        ...state,
        billList: {
          list,
        },
      };
    },
    // 更新数据
    updateModalData(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },
};
